import { AxiosRequestConfig } from 'axios'
import { instance } from './instance'

type Fn = <T>(data: FcResponse<T>) => unknown

interface FcResponse<T> {
  data: T
}

export const request = async <T>(
  config: AxiosRequestConfig<T>,
  callback?: Fn
): Promise<[unknown, FcResponse<T> | null]> => {
  return await new Promise((resolve) => {
    instance(config)
      .then((result) => {
        const res: FcResponse<T> =
          callback != null
            ? (callback(result.data) as FcResponse<T>)
            : (result.data as FcResponse<T>)
        resolve([null, res])
      })
      .catch((err) => {
        resolve([err, null])
      })
  })
}
